package com.light.gulimall.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.light.common.utils.PageUtils;
import com.light.common.utils.Query;
import com.light.common.utils.R;
import com.light.common.vo.WareStockVO;
import com.light.gulimall.ware.config.ProductRemoteServiceClient;
import com.light.gulimall.ware.dao.PurchaseDetailDao;
import com.light.gulimall.ware.dao.WareSkuDao;
import com.light.gulimall.ware.entity.PurchaseDetailEntity;
import com.light.gulimall.ware.entity.WareSkuEntity;
import com.light.gulimall.ware.service.WareSkuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {


    @Autowired
    private PurchaseDetailDao purchaseDetailDao;

    @Autowired
    private ProductRemoteServiceClient productRemoteServiceClient;

    @Autowired
    private WareSkuDao wareSkuDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据条件分页查询商品库存
     *
     * @param params 查询条件
     * @return 查询结果
     */
    @Override
    public PageUtils queryByCondition(Map<String, Object> params) {

        QueryWrapper<WareSkuEntity> wareSkuEntityQueryWrapper = new QueryWrapper<>();

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isBlank(wareId)) {
            wareSkuEntityQueryWrapper.eq("ware_id", wareId);
        }
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isBlank(skuId)) {
            wareSkuEntityQueryWrapper.eq("sku_id", skuId);
        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wareSkuEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 新增或更新库存
     *
     * @param itemIds 采购信息详情表主键id的集合
     */
    @Override
    public void addOrModitfy(List<Long> itemIds) {

        List<PurchaseDetailEntity> purchaseDetailEntities = purchaseDetailDao.selectBatchIds(itemIds);
        for (PurchaseDetailEntity purchaseDetailEntity : purchaseDetailEntities) {
            //查询wms_ware_sku表是否有记录
            QueryWrapper<WareSkuEntity> wareSkuEntityQueryWrapper = new QueryWrapper<>();
            wareSkuEntityQueryWrapper.eq("sku_id", purchaseDetailEntity.getSkuId());
            wareSkuEntityQueryWrapper.eq("ware_id", purchaseDetailEntity.getWareId());
            WareSkuEntity wareSku = getOne(wareSkuEntityQueryWrapper);
            if (ObjectUtils.isEmpty(wareSku)) {
                //新增库存
                WareSkuEntity wareSkuEntity = new WareSkuEntity();
                BeanUtils.copyProperties(purchaseDetailEntity, wareSkuEntity);
                wareSkuEntity.setStock(purchaseDetailEntity.getSkuNum());
                wareSkuEntity.setStockLocked(0);
                //远程调用product模块，查询商品名称sku_name
                //todo 分布式事务
                R info = null;
                try {
                    info = productRemoteServiceClient.info(wareSkuEntity.getSkuId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                LinkedHashMap skuInfo = (LinkedHashMap) info.get("skuInfo");
                wareSkuEntity.setSkuName(skuInfo.get("skuName").toString());
                save(wareSkuEntity);
            } else {
                //修改
                WareSkuEntity wareSkuEntity = new WareSkuEntity();
                wareSkuEntity.setStock(wareSku.getStock() + purchaseDetailEntity.getSkuNum());
                update(wareSkuEntity, wareSkuEntityQueryWrapper);
            }
        }


    }

    @Override
    public List<WareStockVO> queryWareStock(List<Long> skuIds) {
        List<WareSkuEntity> wareSkuEntities = list(new QueryWrapper<WareSkuEntity>().in("sku_id", skuIds));
        return wareSkuEntities.stream().map(wareSkuEntity -> {
            WareStockVO wareStockVO = new WareStockVO();

            Long skuId = wareSkuEntity.getSkuId();
            Long count = wareSkuDao.queryStockBySkuId(skuId);
            wareStockVO.setHasStock(count == null || count > 0);
            wareStockVO.setSkuId(skuId);
            return wareStockVO;
        }).collect(Collectors.toList());
    }

}